/*
 * @Description  : 
 * @Date         : 2021-04-23 15:35:34
 * @Author       : 保锋
 * @LastEditTime : 2021-05-11 18:24:10
 * @LastEditors  : 保锋
 * @FilePath     : \caiying_flutter\lib\services\common_services.dart
 */
import 'package:get/get.dart';
import 'package:yinshui_caiying/http/apis.dart';
import 'package:yinshui_caiying/http/http.dart';
import 'package:yinshui_caiying/model/projectTypeList_model.dart';
import 'package:yinshui_caiying/widgets/picker/models/ad_list_result_model.dart';
import 'package:yinshui_caiying/widgets/picker/models/pickerItem_model.dart';

class CommonService extends GetxService {
  @override
  void onInit() {
    super.onInit();
  }

  Future<CommonService> init() async {
    await _getProjectTypeList();
    // await getAdList('');
    print('加载公共数据服务');
    return this;
  }

  List<PickerItemModel> projectTypeList = [];

  ///获取工程类型列表
  _getProjectTypeList() async {
    try {
      var res = await Http().post(ApiProject.PROJECT_TYPE_LIST,
          data: {"total": 0, "page": 1, "per_page": 1000});
      ProjectTypeListResultModel result =
          ProjectTypeListResultModel.fromJson(res);

      if (result.code == 200) {
        result.content.data.forEach((element) {
          if (element.name == '联户表井') {
            projectTypeList
                .add(PickerItemModel(name: element.name, value: element.id));
          }
        });
      } else {}
    } catch (e) {}
  }

  List<AdItemModel> adList = [];

  ///获取行政区划数据源
  getAdList(String ad_code) async {
    try {
      var res = await Http()
          .post(ApiSystem.SYSTEM_AD_LIST, data: {'ad_code': ad_code});

      AdReslutModel reslut = AdReslutModel.fromJson(res);

      if (reslut.code == 200) {
        adList.clear();
        adList.addAll(reslut.content.data);
      }
    } catch (e) {
      print(e);
    }
  }
}
